﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using Catering.View_Operaction;

namespace Catering.UserTraining.SlrPubbooks
{
    public partial class pubbooks_list : UserIsNull
    {
        public string m_UserID = "0";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserID"] != null) m_UserID = Session["UserID"].ToString();
        }


        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.AspNetPager1.CurrentPageIndex = 1;
            this.LoadRep(this.GetStrWhere());
        }

        //绑定Rep控件
        public void LoadRep(string strwhere, int page)
        {
            this.AspNetPager1.RecordCount = new Catering_Rules.PubBooks().GetRecordCount(strwhere);
            if (page > 0) this.AspNetPager1.CurrentPageIndex = page;
            DataTable dt_PurchaseInfo = new Catering_Rules.PubBooks().GetList(this.AspNetPager1.PageSize, this.AspNetPager1.CurrentPageIndex - 1, strwhere + " order by Bookid desc").Tables[0];//分页处理
            this.Repeater1.DataSource = dt_PurchaseInfo;
            this.Repeater1.DataBind();
        }

        //绑定Rep控件
        public void LoadRep(string strwhere)
        {
            LoadRep(strwhere, 0);
        }

        //获取查询条件
        public string GetStrWhere()
        {
            StringBuilder sb = new StringBuilder(" 1=1 ");

            if (!string.IsNullOrEmpty(this.txt_BookName.Text.Trim()))
            {
                if (this.Drop_SearchType.SelectedValue == "BookName")
                {
                    sb.Append(" and BookName like '%" + this.txt_BookName.Text.Trim() + "%' ");
                }
                else if (this.Drop_SearchType.SelectedValue == "BookType")
                {

                }
            }
            return sb.ToString();
        }

        protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            this.AspNetPager1.CurrentPageIndex = e.NewPageIndex;
            this.LoadRep(this.GetStrWhere());
        }


        /// <summary>
        /// 新增功能
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string url = "~/UserTraining/SlrPubbooks/pubbooks_add.aspx?Flag=ADD";
            Response.Redirect(url);
        }

        protected void btn_ReturnIndex_Click(object sender, EventArgs e)
        {
            string url = "~/UserTraining/index.aspx";
            Response.Redirect(url);
        }

        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int BookID = int.Parse(e.CommandArgument.ToString());
            if (BookID <= 0) return;
            if (e.CommandName == "del")
            {
                if (new Catering_Rules.PubBooks().Delete(BookID))
                {
                    this.LoadRep(this.GetStrWhere());
                    ScriptManager.RegisterStartupScript(this, GetType(), "Affect", "alert('删除成功！')", true);
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, GetType(), "Affect", "alert('删除失败！')", true);
                }
            }
            else if (e.CommandName == "edit")
            {
                string url = "~/UserTraining/SlrPubbooks/pubbooks_add.aspx?Flag=EDIT&BookID=" + BookID;
                Response.Redirect(url);
            }
            else if (e.CommandName == "down")
            {
                Catering_Model.PubBooks _BooksModel = new Catering_Model.PubBooks();
                _BooksModel = new Catering_Rules.PubBooks().GetModel(BookID);
                if (_BooksModel == null) return;
                string url = "~/Download.aspx?topurl=SlrPubbooks/" + _BooksModel.BookAddr + "&toppathtype=books" + "&topname=" + _BooksModel.BookName;
                Response.Redirect(url, false);
            }
        }

        public string GetClassName(string strClassID)
        {
            Catering_Model.ClassInfo _ClassModel = new Catering_Rules.ClassInfo().GetModel(int.Parse(strClassID));
            if (_ClassModel == null) return "";
            return _ClassModel.CName;
        }

        public string GetBookTypeName(string strBookTypeID)
        {
            Catering_Model.BookType _BookTypeModel = new Catering_Rules.BookType().GetModel(int.Parse(strBookTypeID));
            if (_BookTypeModel == null) return "";
            return _BookTypeModel.BookTypeName;
        }

        public string GetUpUserName(string strUserID)
        {
            Catering_Model.user _UserModel = new Catering_Rules.user().GetModel(int.Parse(strUserID));
            if (_UserModel == null) return "";
            return _UserModel.UName;
        }
    }
}